﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace rf200_telecontrol_upper
{
    internal class data
    {
        public static void save_data_dialog(RichTextBox rtb)
        {
            SaveFileDialog saveDataDialog = new SaveFileDialog();
            saveDataDialog.InitialDirectory = System.Windows.Forms.Application.StartupPath;
            string yyyyMMddhh = DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss");
            saveDataDialog.FileName = yyyyMMddhh;
            saveDataDialog.Title = "保存数据文件";
            saveDataDialog.AddExtension = true;
            saveDataDialog.Filter = "RTF文件(*.rtf)|*.rtf";
            //saveDataDialog.Filter = "Excel (*.xlsx)|*.xlsx|Text Files (*.txt)|*.txt|Csv Files (*.csv)|*.csv";
            //openImageDialog.Multiselect = false;
            if (saveDataDialog.ShowDialog() == DialogResult.OK)
            {
                //if (saveDataDialog.FileName.EndsWith(".xlsx"))
                //    new Excell().WriteExcel_Table(saveDataDialog.FileName, mainForm.dataGridView_data);
                //else if (saveDataDialog.FileName.EndsWith(".txt") || saveDataDialog.FileName.EndsWith(".csv"))
                //ExportToExcelOrTxt(dataGridView1, saveDataDialog.FileName);
                rtb.SaveFile(saveDataDialog.FileName);
                MessageBox.Show("导出成功，文件路径为：" + saveDataDialog.FileName);
            }
            else
            { }
        }

        public static void data_decode(byte[] receive_data, int data_length)
        {
            if (data_length > 0)//若成功收到数据
            {
                byte crc_check = ADD8(receive_data, data_length - 1);
                if (crc_check == receive_data[data_length - 1])//若ADD8校验通过
                {
                    if (receive_data[0] == 0x55 && receive_data[data_length - 2] == 0xFF)//数据头
                    {
                        mainForm.key0 = get_bit(receive_data[1], 3);
                        mainForm.left_rocker_key = get_bit(receive_data[1], 2);
                        mainForm.right_rocker_key = get_bit(receive_data[1], 1);
                        mainForm.up = get_bit(receive_data[2], 8);
                        mainForm.down = get_bit(receive_data[2], 7);
                        mainForm.left = get_bit(receive_data[2], 6);
                        mainForm.right = get_bit(receive_data[2], 5);
                        mainForm.X = get_bit(receive_data[2], 4);
                        mainForm.Y = get_bit(receive_data[2], 3);
                        mainForm.A = get_bit(receive_data[2], 2);
                        mainForm.B = get_bit(receive_data[2], 1);
                        mainForm.right_rocker_y = (UInt16)(receive_data[3] << 8 | receive_data[4]);
                        mainForm.right_rocker_x = (UInt16)(receive_data[5] << 8 | receive_data[6]);
                        mainForm.left_rocker_x = (UInt16)(receive_data[7] << 8 | receive_data[8]);
                        mainForm.left_rocker_y = (UInt16)(receive_data[9] << 8 | receive_data[10]);
                    }
                }
            }
        }

        /// <summary>
        /// ADD8校验，参数data为byte数组，data_length
        /// </summary>
        /// <param name="data">校验数据，字节数组</param>
        /// <returns>返回ADD8的值</returns>
        public static byte ADD8(byte[] data, int data_length)
        {
            byte add8 = 0;
            for(int k=0;k<data_length;k++)
                add8 += data[k];
            return add8;
        }

        /// <summary>
        /// 获取一个byte的第几位
        /// </summary>
        /// <param name="data">数据</param>
        /// <param name="num">第几位 1~8</param>
        /// <returns>返回bool值，1=true，0=false</returns>
        private static bool get_bit(byte data, int num)
        {
            num -= 1;
            return (byte)(((0x01 << num) & data) >> num) == 0x01 ? true : false;
        }
    }
}
